
<!--修改弹出框-->
<div class="wizard" id="wizard-edit">
    <form method="post">
        <!--<h1>添加角色</h1>-->
        <div class="wizard-card" data-onValidated="setServerName" data-cardname="name">
            <h3 class="text-center"><span>修改角色权限</span></h3>
            <input type="hidden" name="role_id_edit">
            <div class="wizard-input-section">
                <div class="form-group">
                    <label class="control-label">角色名 <span class="required">*</span>
                    </label>
                    <div>
                        <input type="text" id="role_name" name="role_name" required="required" class="form-control col-md-7 col-xs-12">
                    </div>
                </div>
            </div>
            <div class="wizard-input-section">
                <div class="form-group">
                    <label class="control-label">备注
                    </label>
                    <div>
                        <input type="text" name="remark" class="form-control col-md-7 col-xs-12">
                    </div>
                </div>
            </div>
            <div class="wizard-input-section">
                <div class="form-group">
                    <label class="control-label">状态 <span class="required">*</span></label>
                    <div>
                        <select id="status" class="form-control col-md-7 col-xs-12" name="status" required="required">
                            <option value="1" selected>正常</option>
                            <option value="0">禁用</option>
                        </select>
                    </div>
                </div>
            </div>
            <div class="wizard-input-section">
                <div class="form-group">
                    <label class="control-label">权限 <span class="required">*</span>
                    </label>
                    <div class="zTreeDemoBackground left form-group">
                        <ul id="treeDemo" class="ztree"></ul>
                        <input type="hidden" name="wid">
                    </div>

                </div>
            </div>

        </div>

    </form>
</div>
<!--修改弹出框-->
<script src="__JS__/jquery.js"></script>
<!--ztree-->
<script type="text/javascript" src="__JS__/ztree/jquery.ztree.core.js"></script>
<script type="text/javascript" src="__JS__/ztree/jquery.ztree.excheck.js"></script>
<script type="text/javascript" src="__JS__/ztree/jquery.ztree.exedit.js"></script>
<script>

    function setEditValue(role_id) {
        $.post("{:url('Role/one')}",{role_id:role_id},function(data){

            if(data) {
                $("input[name='role_id_edit']").val(data[0].role_id)
                $("input[name='role_name']").val(data[0].role_name);
                $("input[name='wid']").val(data[0].wid);
                $("input[name='remark']").val(data[0].remark);
                $("select[name='status']").val(data[0].status);
            }
        },'json');
    }
    function edit(role_id, wid) {

        setEditValue(role_id)
        //console.log(widString)
        foredit.show();
        $('.wizard-cancel').text('取消');
        $('.wizard-cancel').addClass('calCancel')
        // $('.wizard-next').addClass('calCancel')
        $('.wizard-next').text('确定');

        /*权限树*/
        var settingEdit = {
            view: {
                selectedMulti: true             /*允许选中多个节点*/
            },
            check: {
                enable: true,
                autoCheckTrigger: true
            },
            data: {
                simpleData: {
                    enable: true
                }
            },
            edit: {
                enable: false
            }
        };
        settingEdit.check.chkboxType = { "Y" : "s", "N" : "s" };

        var datae = '{$jsonways}';
        datae = JSON.parse(datae);
        //console.log(data)
        //获取wid值
        var widString = wid
        //console.log(widString+'12121212')
        /*去掉最后一个逗号*/
        widString = widString.substr(0, widString.length-1)
        var widArr = widString.split(',');
        var widLen = widArr.length;
        /*整数数组*/
        /*var intWid = [];
        intWid = widArr.map(function(data){
            return +data;
        });*/

        var zNodese = [];
        zNodese[datae.length] = {id:0, pId:0, name:all, isParent:true,open:true}
        for(var i=0;i<datae.length;i++){
            for(var j=0;j<widLen;j++){
                //父节点
                if(datae[i].pid == 0){
                    if(datae[i].wid == widArr[j]){
                        datae[i].checked = true;
                    }
                    zNodese[i] = {id:datae[i].wid, pId:datae[i].pid, name:datae[i].w_name, isParent:true, checked:datae[i].checked}
                }else {    //子节点
                    if(datae[i].wid == widArr[j]){
                        datae[i].checked = true;
                    }
                    zNodese[i] = {id:datae[i].wid, pId:datae[i].pid, name:datae[i].w_name, checked:datae[i].checked}
                }
            }
        }


        $.fn.zTree.init($("#treeDemo"), settingEdit, zNodese);
        /*添加的*/
        var treeObje = $.fn.zTree.getZTreeObj("treeDemo");

        var nodese = treeObje.getCheckedNodes()
        //nodes = Json
        //console.log(nodes)
        var trees = '';
        for(var i=0;i<nodese.length;i++){
            var treeNodes = nodese[i]
            if(treeNodes.checked == true){
                trees += treeNodes.id + ',';
            }
            //console.log((treeNodes?treeNodes.name:"root") + "checked " +(treeNodes.checked?"true":"false"));
        }
        //console.log(tree)
        $("input[name='wid']").val(trees)



        var newCount = 1;
        function addHoverDom(treeId, treeNode) {
            var sObj = $("#" + treeNode.tId + "_span");
            if (treeNode.editNameFlag || $("#addBtn_"+treeNode.tId).length>0) return;
            /*var addStr = "<span class='button add' id='addBtn_" + treeNode.tId
                + "' title='add node' onfocus='this.blur();'></span>";
            sObj.after(addStr);*/
            var btn = $("#addBtn_"+treeNode.tId);
            if (btn) btn.bind("click", function(){
                var zTree = $.fn.zTree.getZTreeObj("treeDemo");
                zTree.addNodes(treeNode, {id:(100 + newCount), pId:treeNode.id, name:"new node" + (newCount++)});
                return false;
            });
        }
        function removeHoverDom(treeId, treeNode) {
            $("#addBtn_"+treeNode.tId).unbind().remove();
        }

        foredit.on("submit", function(wizard) {
            setTimeout(function() {
                wizard.trigger("success");
                wizard.hideButtons();
                wizard._submitting = false;
                wizard.showSubmitCard("success");
                wizard.updateProgressBar(0);
            }, 2000);
            esave();
            foredit.reset().close();
            setTimeout(function () {
                //刷新页面
                window.location.reload();
            },1000)
        });

    }
    function esave() {
        var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
        var nodes = treeObj.getCheckedNodes();
        //console.log(nodes,'nodesnodesnodesnodes')

        var tree = '';
        for(var i=0;i<nodes.length;i++){
            var treeNode = nodes[i]
            if(treeNode.checked == true){
                tree += treeNode.id + ',';
            }
            //console.log((treeNode?treeNode.name:"root") + "checked " +(treeNode.checked?"true":"false"));
        }
        //console.log(tree)
        $("input[name='wid']").val(tree)

        var wid = $("input[name='wid']").val();
        var role_id = $("input[name='role_id_edit']").val();
        var role_name = $("input[name='role_name']").val();
        var remark = $("input[name='remark']").val();
        var status = $("select[name='status']").val();
        var data = {'wid':wid,'role_id':role_id,'role_name':role_name,'remark':remark,'status':status};
        //console.log(data)
        $.ajax({
            //几个参数需要注意一下
            type: "POST",//方法类型
            dataType: "json",//预期服务器返回的数据类型
            url: "{:url('Role/esave')}",//url
            data: data,
            success: function (result) {
                //console.log(result)
                var code = result.code
                var msg = result.msg
                changeText(msg, code)
                //console.log('123'+code)
            },
            error:function (result) {
                var code = 0
                var msg = '服务器错误';
                changeText(msg, code)
                return false;
            }
        });
    }

</script>
